package com.ruoyi.web.shopping.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.shopping.dto.ProductsDTO;
import com.ruoyi.web.shopping.entity.Products;
import com.ruoyi.web.shopping.service.ProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/products")
@Slf4j
@Api(tags = "商品相关接口")
public class ProductsController {

    @Autowired
    private ProductsService productsService;

    /**
     * 新增商品
     *
     * @param products
     * @return
     */
    @PostMapping(value = "/save")
    @ApiOperation("新增商品")
    public R<String> save(@RequestBody Products products) {
        log.info("新增商品：{}", products);
        productsService.save(products);
        return R.ok("新增成功");
    }

    /**
     * 删除商品（逻辑删除）
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除商品")
    public R<String> deleteById(@PathVariable String id) {
        log.info("删除商品id为：{}", id);
        productsService.deleteById(id);
        return R.ok("删除成功");
    }

    /**
     * 修改商品
     *
     * @param
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改商品")
    public R<String> update(@RequestBody ProductsDTO productsDTO) {
        log.info("修改商品为：{}", productsDTO);
        productsService.update(productsDTO);
        return R.ok("修改成功");
    }

    /**
     * 查询商品
     *
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    @ApiOperation("查询商品")
    public R<ProductsDTO> selectById(@PathVariable String id) {
        log.info("查询商品id为{}", id);
        ProductsDTO productsDTO = productsService.selectById(id);
        return R.ok(productsDTO);
    }

    /**
     * 根据商品名查询
     *
     * @param name
     * @return
     */
    @GetMapping("/selectby/{name}")
    @ApiOperation("通过名称查询商品")
    public R<ArrayList<ProductsDTO>> selectByName(@PathVariable String name) {
        log.info("查询商品名称为：{}", name);
        ArrayList<ProductsDTO> productsDTOs = productsService.selectByName(name);
        return R.ok(productsDTOs);
    }
}
